﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('ServiceWorkOrderMain.Add')" type="primary" icon="plus" @click="handleAdd()">发起工单
      </a-button>
      <a-button
        v-if="hasPerm('ServiceWorkOrderMain.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除
      </a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="SWOTitle">工单标题</a-select-option>
                <a-select-option key="CustomerName">客户名称</a-select-option>
                <a-select-option key="RelationNo">关联单号</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" @keydown.enter="getDataList"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
            >查询
            </a-button>
            <a-button style="margin-left: 8px" @click=";(queryParam = { condition: 'SWOTitle' })">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: tableHeight }"
      :columns="columns"
      :rowKey="(row) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small">
      <template slot="State" slot-scope="text">
        <a-tag
          :color="getColorAndContent(text,StateInfos).color">
          {{ getColorAndContent(text, StateInfos).content }}
        </a-tag>
      </template>
      <span slot="SWOType" slot-scope="text">
        <a-tag :color="getColorAndContent(text,SWOTypeInfos).color">{{ text }}</a-tag>
      </span>
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('ServiceWorkOrderMain.View')" @click="dele(record,'查看工单')">查看</a>
          <a-divider
            v-if="hasPerm('ServiceWorkOrderMain.View') && hasPerm('ServiceWorkOrderMain.Edit')&&record.State<2"
            type="vertical"
          />
          <a v-if="hasPerm('ServiceWorkOrderMain.Edit')&&record.State<2" @click="handleEdit(record, '编辑工单')">编辑</a>
          <a-divider v-if="hasPerm('ServiceWorkOrderMain.Delete')&&record.State<2" type="vertical"/>
          <a v-if="hasPerm('ServiceWorkOrderMain.Delete')&&record.State<2" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
      <span slot="ExpenseInfo" slot-scope="text, record" v-format="'#,##0.00'">{{ getExpenseInfo(record.SWODList) }}</span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
    <dele ref="del" :parentObj="this"></dele>

  </a-card>
</template>

<script>
import EditForm from './EditForm'
import dele from './dele.vue'

import moment from 'moment'

const columns = [
  { title: '工单标题', dataIndex: 'SWOTitle', width: '220px' },
  {
    title: '工单类型',
    dataIndex: 'SWOType',
    scopedSlots: { customRender: 'SWOType' },
    width: '5%',
    align: 'center'
  },
  { title: '关联单号', dataIndex: 'RelationNo', width: '100px' },
  {
    title: '开始日期',
    width: '6%',
    dataIndex: 'StartTime',
    customRender: (text) => {
      return text ? text.substring(0, 10) : ''
    }
  },
  { title: '客户', dataIndex: 'CustomerName', width: '220px' },
  {
    title: '执行人',
    width: '150px',
    dataIndex: 'SWOAList',
    customRender: (arr) => {
      return arr.length > 0 ? arr.map((item) => item.ExecutorName).join(',') : ''
    }
  },
  {
    title: '费用',
    dataIndex: 'SWODList',
    width: '100px',
    align: 'right',
    scopedSlots: { customRender: 'ExpenseInfo' }
  },
  {
    title: '状态',
    dataIndex: 'State',
    width: '4%',
    align: 'center',
    scopedSlots: { customRender: 'State' }
  },
  { title: '操作', width: '100px', dataIndex: 'action', scopedSlots: { customRender: 'action' } },
]

export default {
  components: {
    EditForm,
    dele
  },
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(true) + 24
    })
    this.getDataList()
  },
  data () {
    return {
      tableHeight: 570, // 表格高度
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true,
      },
      filters: {},
      sorter: { field: 'StartTime', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'SWOTitle' },
      selectedRowKeys: [],
      StateInfos: [
        { text: '0', color: 'cyan', content: '发起' },
        { text: '1', color: 'blue', content: '启动' },
        { text: '2', color: 'orange', content: '执行' },
        { text: '3', color: 'red', content: '超时' },
        { text: '4', color: 'green', content: '关闭' }
      ],
      SWOTypeInfos: [
        { text: '投诉处理', color: 'red' },
        { text: '客户跟进', color: 'orange' },
        { text: '样品服务', color: 'blue' },
        { text: '技术服务', color: 'purple' }
      ]
    }
  },
  methods: {
    dele (record, title) {
      this.$refs.del.openForm(record, title)
    },
    enter (e) {
      if (e.keyCode === 13) {
        this.getDataList()
      }
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/SWOManagement/ServiceWorkOrderMain/GetDataListByMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: [this.queryParam],
          ...this.filters
        })
        .then((resJson) => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '发起工单', true)
    },
    handleEdit (record, title) {
      this.$refs.editForm.openForm(record, title)
    },
    handleDelete (ids) {
      const thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/SWOManagement/ServiceWorkOrderMain/DeleteData', ids).then((resJson) => {
              resolve()
              if (resJson.Success) {
                thisObj.$message.success('操作成功!')
                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    getColorAndContent (text, items) {
      for (let i = 0; i < items.length; i++) {
        if (items[i].text === text) {
          return items[i]
        }
      }
    },
    getExpenseInfo (items) {
      if (items) {
        let ExpenseInfo = 0
        for (let i = 0; i < items.length; i++) {
          ExpenseInfo += items[i].ExpenseInfo
        }
        return ExpenseInfo
      }
      return 0
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
